package main

import (
	"fmt"
	"github.com/hpcloud/tail"
	"log"
	"time"
)

const filename = "app.log"

func main() {

	config := tail.Config{
		ReOpen:    true,
		Follow:    true,
		Location:  &tail.SeekInfo{Offset: 0, Whence: 2},
		MustExist: false,
		Poll:      true,
	}

	tailFile, err := tail.TailFile(filename, config)
	if err != nil {
		log.Fatalln(err)
	}

	var (
		msg *tail.Line
		ok  bool
	)

	for {
		msg, ok = <-tailFile.Lines
		if !ok {
			log.Printf("读取[%s]失败, 稍等一秒钟\n", filename)
			time.Sleep(time.Second)
			continue
		}
		fmt.Println(msg.Text)
	}

}
